
import WoundClosure
from backend.backend import Backend
from workflow.task import Task
from workflow.result import Result 
from workflow.flowData import Status


class ORBackend(Backend):
    
   
    def __init__(self): 
        # Specify the workflow name:       
        Backend.__init__(self, "Team1OR")  # !!! be sure you update the workflow name

    def wire(self):
     
        self.register_result_listener("Surgeon", "StartSurgery", self.surgery_started)
        self.register_result_listener("Surgeon","RequestInstrument", self.instrument_requested)
        self.register_result_listener("Technician","TakePatient", self.take_patient)
        self.register_result_listener("Nurse","HandInstrument", self.instrument_handed)
        self.register_result_listener("Surgeon","IsAnotherInstrument", self.is_another_instrument_requested)
        self.register_result_listener("Surgeon","IsWoundClosure", self.is_wound_closed)
        self.register_result_listener("Nurse","WoundClosure", self.wound_closed)
        self.register_result_listener("Surgeon","WoundClosure", self.wound_closed)
        
        

    def surgery_started(self, results):
        for result in results:  # repeat the following actions for each result
            # !!! Fix the line below...
            task = Task.construct_from_result(result,"Surgeon", "RequestInstrument")
            task.set_field('InstrumentNumber', 0);
            self.workflow.add(task) # add the new task to the workflow
            self.workflow.update_status(result, Status.COMPLETE)

    def is_another_instrument_requested(self, results):
        for result in results:  # repeat the following actions for each result
            if result.get_bool_field("IsAnotherInstrument"):
                task = Task.construct_from_result(result,"Surgeon", "RequestInstrument")
                task.set_field("InstrumentNumber", result.get_int_field("InstrumentNumber")+1)
            else:
                task = Task.construct_from_result(result,"Surgeon", "IsWoundClosure")
            self.workflow.add(task) 
            self.workflow.update_status(result, Status.COMPLETE)
                
    def instrument_requested(self, results):    
        for result in results:  # repeat the following actions for each result
            task = Task.construct_from_result(result,"Nurse", "HandInstrument")
            self.workflow.add(task) # add the new task to the workflow
            self.workflow.update_status(result, Status.COMPLETE)

    def instrument_handed(self, results):    
        for result in results:  # repeat the following actions for each result
            task = Task.construct_from_result(result,"Surgeon", "IsAnotherInstrument")
            self.workflow.add(task) # add the new task to the workflow
            self.workflow.update_status(result, Status.COMPLETE)

    def is_wound_closed(self, results):    
        for result in results: 
            if result.get_bool_field("IsWoundClosure"):
                task = Task.construct_from_result(result,"Surgeon", "WoundClosure")
            else:
                task = Task.construct_from_result(result,"Nurse", "WoundClosure")
            self.workflow.add(task) 
            self.workflow.update_status(result, Status.COMPLETE)
            
    def wound_closed(self, results):    
        for result in results: 
            task = Task.construct_from_result(result,"Technician", "TakePatient")
            self.workflow.add(task) 
            self.workflow.update_status(result, Status.COMPLETE)
           

    def take_patient(self, results):    
        for result in results:  # repeat the following actions for each result
            self.workflow.update_status(result, Status.COMPLETE)
    
            
if __name__ == '__main__':
    backend = ORBackend()